package com.xuchaotong.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 357070 on 2017/4/8.
 */
public class TreeTools {


    public List<Node> buildTree() {
        List<Node> tree = new ArrayList<Node>();
        List<Node> rootNodes = getRootNodes();

        for(Node node : rootNodes){
            buildChildNodes(node);

            tree.add(node);
        }

        return tree;
    }

    private List<Node> getRootNodes() {
        return getChildNodes(null);
    }

    private List<Node> getChildNodes(Node parentNode) {
        return null;
    }


    //递归生成树结构，通过父节点查找子节点列表
    private void buildChildNodes(Node parentNode) {
        List<Node> childNodes = getChildNodes(parentNode);
        if (!childNodes.isEmpty()) {
            for (Node childNode : childNodes) {
                buildChildNodes(childNode);
            }

            parentNode.setChildNodes(childNodes);
        }
    }


}
